import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/api/http_server/dictionaries.dart';
import 'package:loxen/utils/config.dart';
import 'package:provider/provider.dart';
import 'package:loxen/mode/master/file_loxen.dart';
import 'package:loxen/view/master/file_inspiration_list.dart';
import 'package:loxen/view/master/file_material_list.dart';
import 'package:loxen/view/master/file_picture_list.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:loxen/viewmode/file_info_viewmodel.dart';
import 'package:loxen/widget/empty_widget.dart';
import 'package:loxen/widget/fail_widget.dart';
import 'package:loxen/widget/loading_widget.dart';
import 'package:loxen/widget/my_subassembly.dart';

class FileInfoWidget extends StatelessWidget {
  final FileLoxen file;
  final FileType type;
  final bool isMe;
  final String userId;

  const FileInfoWidget(
      {Key key, this.file, this.type, this.isMe = true, this.userId})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<FileInfoViewModel>(
      create: (_) {
        FileInfoViewModel model =
            FileInfoViewModel(file, type, userId);
        model.loadData();
        return model;
      },
      child: Scaffold(
        appBar: PreferredSize(
          child: Container(
            color: Theme.of(context).backgroundColor,
            child: SafeArea(
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 2,
                    child:
                        Row(
                          children: <Widget>[
                            MySubassembly.getBackIcon(context, color: Colors.black),
                          ],
                        ),
                  ),
                  Expanded(
                    flex: 4,
                    child: Center(
                      child: Text(
                        file.name,
                        style: TextStyle(
                            fontSize: 32.sp,
                            color: Theme.of(context).textTheme.subtitle2.color),
                      ),
                    ),
                  ),
                  Expanded(
                      flex: 2,
                      child: Builder(builder: (context) {
                        if (isMe) {
                          var data = context.select<FileInfoViewModel, bool>(
                              (value) => value.isDelete);
                          return data
                              ? _deleteWidget(context)
                              : Row(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: <Widget>[
                                    MySubassembly.getSearchIcon(() {
                                      context
                                          .read<FileInfoViewModel>()
                                          .isDelete = true;
                                      file.num = context
                                          .read<FileInfoViewModel>()
                                          .list
                                          .length;
                                    },
                                        url: "images/trash_can.png",
                                        width: 31.w,
                                        height: 31.w),
                                  ],
                                );
                        } else {
                          return Constants.empty;
                        }
                      }))
                ],
              ),
            ),
          ),
          preferredSize: Size(double.infinity, 95.h),
        ),
        body: Selector<FileInfoViewModel, BaseState>(
            builder: (context, state, __) {
              if (state == BaseState.LOADING) {
                return LoadingWidget();
              }
              if (state == BaseState.EMPTY) {
                return EmptyWidget();
              }
              if (state == BaseState.FAIL) {
                return FailWidget(onClick: () {
                  Provider.of<FileInfoViewModel>(context, listen: false)
                      .loadData();
                });
              }
              if (state == BaseState.CONTENT) {
                var child;
                if (type == FileType.INSPIRATION) {
                  child = const FileInspirationList();
                } else if (type == FileType.MATERIAL) {
                  child = const FileMaterialList();
                } else {
                  child = FilePictureList();
                }
                return Container(
                  color: Theme.of(context).backgroundColor,
                  child: child,
                );
              }
              return Text('');
            },
            selector: (_, model) => model.mState),
      ),
    );
  }

  Widget _deleteWidget(BuildContext context) {
    ValueNotifier<bool> _allDelete = ValueNotifier(false);
    return Material(
      color: Colors.transparent,
      child: IconButton(
          icon: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              ValueListenableProvider<bool>.value(
                value: _allDelete,
                builder: (context, _) {
                  return context.watch<bool>()
                      ? Image.asset(
                          'images/select@2x.png',
                          fit: BoxFit.cover,
                          width: 28.w,
                          height: 28.w,
                        )
                      : Container(
                          width: 28.w,
                          height: 28.w,
                          decoration: BoxDecoration(
                              border: Border.all(
                                  color: Colors.black26, width: 0.5)),
                        );
                },
              ),
              Padding(
                padding: EdgeInsets.only(left: 5.w),
                child: Text(
                  '全选',
                  style: TextStyle(fontSize: 28.sp, color: Colors.black26),
                ),
              )
            ],
          ),
          onPressed: () {
            if (_allDelete.value) {
              context.read<FileInfoViewModel>().unSelectAll();
            } else {
              context.read<FileInfoViewModel>().selectAll();
            }
            _allDelete.value = !_allDelete.value;
          }),
    );
  }
}
